<?php

session_start();
setcookie(session_name(), session_id(), time()+3600*24*365, "/");

require_once './include/common.inc.php';
require_once 'left.php';
require_once 'right.php';
include_once './include/cart.class_p.php';

$page_title = 'Acuity World WebShop';
$menu_id = 0;
$menu_str = PrintMenu();
$check_out = isset($_GET['checkout']) || isset($_GET['modify']) ? 1 : 0;

if (!isPromoterLogin()) 
{
	header ("location:promoter_login.php");
	exit;
}	
if($promoter_faq1=="0")//promoter???????????????????
{
	header ("location:message.php?id=1");
	exit;
}

//echo "<pre>";print_r($_POST);print_r($_SESSION);
$cart = new cart();
$vat_u=getVat();//???vat ,??PRODUCT?vat?-1,???vat;
$cms=getCms($promoter_uid);
$cart->setPromoterOsd($cms["cms_osd1"]);//????????	

$paymentBOX['baseURL']			= 'https://betaling.wannafind.dk/';
$paymentBOX['proxyURL']			= $paymentBOX['baseURL'].'proxy/p.php/';
$pay_url = "promoter_payment.php";
$pay_amount=0;

$sql="SELECT * FROM  `promoter_info` WHERE  `id` = '$promoter_uid'";
$user_info_ary = $db->get_one($sql);
$phone = str_replace("||", " " , $user_info_ary['phone']);
$mobile = str_replace("||", " " , $user_info_ary['mobile']);
$fax = str_replace("||", " " , $user_info_ary['fax']);
$faq2_checked = $user_info_ary['faq2'] == 1 ? "checked" : "";



if (empty($cart->myCart) && isset($_GET['modify'])) {
	if (!$cart = unserialize(getPromoterCartInfo($promoter_uid))) {
		$cart = new cart();
	} else {
		$set_country = $cart->getCountry();
		$cart->update();            
	}
}


$pid = intval($_GET['pid']);
$act = isset($_GET['act']) ? $_GET['act'] : $_POST['act'];

if (isset($_POST['set_country']) &&  $_POST['set_country']>=0 && $_POST['set_country']<=3) {
    $set_country = $_POST['set_country'];
} elseif (isset($_SESSION['myCart_country']) &&  $_SESSION['myCart_country']>=0 && $_SESSION['myCart_country']<=3) {
    $set_country = $_SESSION['myCart_country'];
} else {
    $set_country = 0;
}

// ?
if ($act == 'cart_update') {
    if (!empty($_POST['update_qty_ary'])) {
        foreach ($_POST['update_qty_ary']  as $k=>$v) {
            if (!is_numeric($v)) {
            } else if ($v <= 0) {
                $cart->emptyOne($k);
            } else {
                $cart->modifyCount($k, $v);
            }
        }
    }
}

// hpro_product?
if ($act == 'addone' && $pid>0) {

    $sql = "SELECT `name`, `price`, `short_desc`, `vat`, `weight`, `is_special`,`stock_status` FROM  `pro_product` WHERE  `id` = '$pid'";
    $rs = $db->get_one($sql);

    $sql = "SELECT t.`name`, t.`id` FROM  `pro_product_type` pt, `pro_type` t WHERE  pt.`type_id` = t.`id` AND pt.`product_id` = '$pid' GROUP BY t.`id`";
    $query = $db->query($sql);
    while($rs2 = $db->fetch_array($query)) {
        $type_ary_1[$rs2['id']] = $rs2['name'];
    }
    $type_name = '';
    if (!empty($type_ary_1)) {
        $type_name = implode("<br />", $type_ary_1);
    }
	$pro_vat=$rs['vat'];
	if($pro_vat<0)
		$pro_vat=$vat_u;
	if($rs["stock_status"]=="1")
	{
		$cart->setCountry($set_country);
		$cart->addOne($pid, $rs['name'], $type_name, $rs['price'], $rs['short_desc'],$pro_vat, $rs);
	}
	
    header ("location:shopping_cart_p.php");
    exit;
}

// hcourse_schedule?
if ($act == 'addcourse' && $pid>0) {
    $cart->setCountry($set_country);

    db2Init();
    $sql = "SELECT `name`, `price`, `datedetails`, `date`, `vat` FROM  `course_schedule` WHERE  `id` = '$pid'";
    $rs = $db2->get_one($sql);
    $rs['weight'] = 0;

    $date_ary = explode("-", $rs['date']);
    $rs['start_on'] = mktime(0, 0, 0, $date_ary[1], $date_ary[2], $date_ary[0]);

    $cart->addOne($pid, $rs['name'].' (Start on '.$rs['date'].')', 'Courses / Programs', $rs['price'], $rs['datedetails'], $vat_u, $rs);

    header ("location:shopping_cart_p.php");
    exit;
}

// ?h?
if ($act == 'removeone' && $pid>0) {
    $cart = new cart();
    $cart->emptyOne($pid);
    header ("location:shopping_cart_p.php");
    exit;
}

$cart->setCountry($set_country);
$cart->update();
$info_ary = $cart->getData();
$total_qty = $cart->getSortCount();//????
$total_cost = $cart->getTotalCost();//?????
$cart_discount = $cart->getDisCount();//????
$new_total_cost=number_format($total_cost-$cart_discount,2,".","") ;
$total_cost2 = $cart->getTotalCost2();//??????(????,????,????)
$cart_addvat = $cart->getAddVat();
$cart_addshippingfee = $cart->getAddShippingFee();


$cms_outstanding=$cms["cms_osd1"]-$total_cost2;//????????????
$cms_outstanding=number_format($cms_outstanding,2,'.','');

$pay_amount = $cart->getPayAmount();
if($pay_amount <= 0)//????????????
	$pay_url="promoter_checkout.php";

//$sql_vat="select vat from pro_product ";
//$query_vat=$db->query($sql_vat);
//while($rs_vat=$db->fetch_array($query_vat))
//{
//global $vat;
//$vat=$rs_vat['vat'];
//}
//
//$cart_addvat=(float)$total_cost*$vat/100;
require_once PrintEot('header');
require_once PrintEot('left');
require_once PrintEot('shopping_cart_p');
require_once PrintEot('right');
require_once PrintEot('footer');

?>